// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const app = getApp();
// 登录验证
app.cp_login_if()
Page({
  data: {
   temp:0,
   temp_max:0,
   humi:0,
   light:0,
   NH3:0,
   device:0
    },
  getinfo(){
  var that = this
  wx.request({
    url: 'https://iot-api.heclouds.com/datapoint/history-datapoints?product_id=0jvvxqbO6m&device_name=jishe_2',
    header: {
      "Authorization": "version=2018-10-31&res=products%2F0jvvxqbO6m%2Fdevices%2Fjishe_2&et=1745126236&method=md5&sign=pCqJ64Pa5nPsNUt2Vfg3HA%3D%3D"
    },
    method: "GET",
    success: function (e) {
      console.log("获取成功",e)
      that.setData({
        temp:e.data.data.datastreams[2].datapoints[0].value,
        humi:e.data.data.datastreams[7].datapoints[0].value,
        light:e.data.data.datastreams[1].datapoints[0].value,
        NH3:e.data.data.datastreams[5].datapoints[0].value,
        temp_max:e.data.data.datastreams[3].datapoints[0].value,
      })
      console.log("temp==",that.data.temp)
    }
  });
  },

  getdevinfo(){
    var that = this
    wx.request({
      url: 'https://iot-api.heclouds.com/device/detail?product_id=0jvvxqbO6m&device_name=jishe_2',
      header: {
        "Authorization": "version=2018-10-31&res=products%2F0jvvxqbO6m%2Fdevices%2Fjishe_2&et=1745126236&method=md5&sign=pCqJ64Pa5nPsNUt2Vfg3HA%3D%3D"
      },
      method: "GET",
      success: function (e) {
        console.log("获取设备信息成功",e)
        that.setData({
          device:e.data.data.status
        })
        console.log("temp==",that.data.temp)
      }
    });
    },

  open_led:function(){
    wx.request({
      url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
      header: {
        "content-type":"application/json",
        "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
      },
      method:"POST",
      data:{"Led_state":1},
      success(res){
        console.log("成功",res.data)
      },
      fail(res){
        console.log("失败",res)
      }
    })
  },

  close_led:function(){
    wx.request({
      url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
      header: {
        "content-type":"application/json",
        "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
      },
      method:"POST",
      data:{"Led_state":0},
      success(res){
        console.log("成功",res.data)
      },
      fail(res){
        console.log("失败",res)
      }
    })
  },

  open_fan:function(){
    wx.request({
      url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
      header: {
        "content-type":"application/json",
        "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
      },
      method:"POST",
      data:{"Fan_state":1},
      success(res){
        console.log("成功",res.data)
      },
      fail(res){
        console.log("失败",res)
      }
    })
  },

  close_fan:function(){
    wx.request({
      url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
      header: {
        "content-type":"application/json",
        "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
      },
      method:"POST",
      data:{"Fan_state":0},
      success(res){
        console.log("成功",res.data)
      },
      fail(res){
        console.log("失败",res)
      }
    })
  },

  open_window:function(){
    wx.request({
      url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
      header: {
        "content-type":"application/json",
        "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
      },
      method:"POST",
      data:{"Window_state":1},
      success(res){
        console.log("成功",res.data)
      },
      fail(res){
        console.log("失败",res)
      }
    })
  },

  close_window:function(){
    wx.request({
      url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
      header: {
        "content-type":"application/json",
        "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
      },
      method:"POST",
      data:{"Window_state":0},
      success(res){
        console.log("成功",res.data)
      },
      fail(res){
        console.log("失败",res)
      }
    })
  },

  fan:function(e){
    // console.log("是否选中：",e.detail.value)
    var status = e.detail.value
    if(status==true){
      console.log("开风扇")
        wx.request({
        url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
        header: {
          "content-type":"application/json",
          "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
        },
        method:"POST",
        data:{Fan_state:1},
        success(res){
          console.log("成功",res.data)
        },
        fail(res){
          console.log("失败",res)
        }
      })
    }
    else{
      console.log("关风扇")
      wx.request({
      url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
      header: {
        "content-type":"application/json",
        "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
      },
      method:"POST",
      data:{Fan_state:0},
      success(res){
        console.log("成功",res.data)
      },
      fail(res){
        console.log("失败",res)
      }
    })
    }
  },

  led:function(e){
    // console.log("是否选中：",e.detail.value)
    var status = e.detail.value
    if(status==true){
      console.log("开灯")
        wx.request({
        url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
        header: {
          "content-type":"application/json",
          "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
        },
        method:"POST",
        data:{Led_state:1},
        success(res){
          console.log("成功",res.data)
        },
        fail(res){
          console.log("失败",res)
        }
      })
    }
    else{
      console.log("关灯")
      wx.request({
      url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
      header: {
        "content-type":"application/json",
        "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
      },
      method:"POST",
      data:{Led_state:0},
      success(res){
        console.log("成功",res.data)
      },
      fail(res){
        console.log("失败",res)
      }
    })
    }
  },

  window:function(e){
    // console.log("是否选中：",e.detail.value)
    var status = e.detail.value
    if(status==true){
      console.log("开窗户")
        wx.request({
        url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
        header: {
          "content-type":"application/json",
          "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
        },
        method:"POST",
        data:{Window_state:1},
        success(res){
          console.log("成功",res.data)
        },
        fail(res){
          console.log("失败",res)
        }
      })
    }
    else{
      console.log("关窗户")
      wx.request({
      url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
      header: {
        "content-type":"application/json",
        "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
      },
      method:"POST",
      data:{Window_state:0},
      success(res){
        console.log("成功",res.data)
      },
      fail(res){
        console.log("失败",res)
      }
    })
    }
  },

  decrease:function(e){
    if(this.data.temp_max == 1){
      wx.showToast({
        title: '不能再减了~',
        icon:'none'
      })
      return ;
    }
    var Num = this.data.temp_max - 1;
    this.setData({
      temp_max: Num
    })
    wx.request({
      url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
      header: {
        "content-type":"application/json",
        "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
      },
      method:"POST",
      data:{"Temp_max":0},
      success(res){
        console.log("成功",res.data)
      },
      fail(res){
        console.log("失败",res)
      }
    })
  },
  increase:function(e){
    var Num = this.data.temp_max + 1;
    this.setData({
      temp_max: Num
    })
    wx.request({
      url: 'https://iot-api.heclouds.com/datapoint/synccmds?product_id=0jvvxqbO6m&device_name=jishe_2&timeout=30',
      header: {
        "content-type":"application/json",
        "Authorization": "version=2022-05-01&res=userid%2F352102&et=1745126236&method=md5&sign=7nEhI5PUb8O1dMP7q0idpA%3D%3D"
      },
      method:"POST",
      data:{"Temp_max":1},
      success(res){
        console.log("成功",res.data)
      },
      fail(res){
        console.log("失败",res)
      }
    })
  },

  onLoad() {
    var that = this
    setInterval(function(){
      that.getinfo()
      that.getdevinfo()
    },3000)
   
  }
  
})
